﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.CargoOwnerCommand;
using T.Basic.Read.API.Appliction.Command.LineCommand;
using T.Basic.Read.API.Appliction.Command.VehicleCommand;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace T.Basic.Read.API.Controllers
{
    /// <summary>
    /// 车辆管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//JWT验证
    [ApiController]
    public class VehiclesController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public VehiclesController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示车辆信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<Vehicle>>> GetCargoOwnerAll([FromQuery] VehicleListCommand command)
        {
            ApiResult<PageDto<Vehicle>> result = new ApiResult<PageDto<Vehicle>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
            //try
            //{
            //    command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
            //    return await _mediator.Send(command);
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }
        /// <summary>
        /// 显示车辆车牌号
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<ApiResult<List<Vehicle>>> GetVehicleShowPlateNumber()
        {
            ApiResult<List<Vehicle>> result = new ApiResult<List<Vehicle>>();
            try
            {
                
                result.Data = await _mediator.Send(new VehicleShowPlateNumberCommand());
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
            //try
            //{
            //    return await _mediator.Send<List<Vehicle>>(new VehicleShowPlateNumberCommand());
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }
        /// <summary>
        /// 导出车辆文档
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<IActionResult> GetExcelName([FromQuery] VehicleListCommand command)
        {
            try
            {
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("车辆列表");
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("厂牌型号");
                row.CreateCell(1).SetCellValue("车牌号");
                row.CreateCell(2).SetCellValue("司机姓名");
                row.CreateCell(3).SetCellValue("所属公司");
                row.CreateCell(4).SetCellValue("车型");
                row.CreateCell(5).SetCellValue("车辆颜色");
                row.CreateCell(6).SetCellValue("购置日期");
                row.CreateCell(7).SetCellValue("营运证号");
                row.CreateCell(8).SetCellValue("保险到期时间");
                row.CreateCell(9).SetCellValue("年检到期时间");
                row.CreateCell(10).SetCellValue("保养公里设置");
                var fueList = await _mediator.Send(command);
                int index = 1;
                foreach (var item in fueList.list)
                {
                    row = sheet.CreateRow(index);
                    row.CreateCell(0).SetCellValue(item.BrandAndModel);
                    row.CreateCell(1).SetCellValue(item.PlateNumber);
                    row.CreateCell(2).SetCellValue(item.DriverName);
                    row.CreateCell(3).SetCellValue(item.BelongingCompany);
                    row.CreateCell(4).SetCellValue((double)(item.VehicleLong+item.VehicleWide+item.VehicleTall));
                    row.CreateCell(5).SetCellValue(item.VehicleColor);
                    row.CreateCell(6).SetCellValue(item.AcquisitionDate.ToString());
                    row.CreateCell(7).SetCellValue(item.TradingCard);
                    row.CreateCell(8).SetCellValue(item.InsuranceExpirationDate.ToString());
                    row.CreateCell(9).SetCellValue(item.AnnualInspectionExpiratDate.ToString());
                    row.CreateCell(10).SetCellValue((double)item.MaintenanceMileageSetting);
                    index++;
                }

                //导出
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                string excelMine = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, excelMine, "车辆列表.xlsx");

            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}
